//
//  ChangjingCaipuVc.m
//  Hand-held_kitchen
//
//  Created by 张张烨 on 16/1/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ChangjingCaipuVc.h"
#import "changjingCell.h"
#import "changjingMOble.h"
#import "ZongTouwenjian.pch"
#import "ChangjingWebView.h"
#import "ZZYTAFetwork.h"



@interface ChangjingCaipuVc ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UICollectionView *collection;
@property(nonatomic,retain)NSMutableArray *dataArr; // 数组
@property(nonatomic,assign)BOOL isSj;
@property(nonatomic,assign)NSInteger i;
@end
@implementation ChangjingCaipuVc
- (void)dealloc
{
    [_collection release];
    [_dataArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 返回导航栏透明
     [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];
    _i = 0;
  
    [self data];
    [self collection1];
    
    
    
//    [self jiazai];
    
//    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
//    button.frame = CGRectMake(50, 50, 50, 50);
//    button.backgroundColor = [UIColor redColor];
//    [button setTitle:@"返回" forState:(UIControlStateNormal)];
//    [self.view addSubview:button];
//    [button addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchDragInside)];
       [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bofang"] forBarMetrics:UIBarMetricsCompactPrompt];
}


-(void)button:(UIButton*)bu
{
//    self.hidesBottomBarWhenPushed = NO;
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

#










// 数据
-(void)data
{
    NSString *str =[NSString stringWithFormat:@"http://api.izhangchu.com/?size=20&page=1&methodName=SceneList&version=4.3&"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"data"];
        self.dataArr = [changjingMOble modelHanderWithArray:arr];
        
        
      
        [_collection reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}



-(void)collection1
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
     layout.itemSize = CGSizeMake(Width / 3, (Height - 44 - 22 -60) / 3);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.collection = [[UICollectionView alloc]initWithFrame:Frame collectionViewLayout:layout];
    self.collection.dataSource  =self;
    self.collection.delegate =self;
    _collection.backgroundColor = [UIColor whiteColor];
//    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
   
    [self.view addSubview:_collection];
    
    [_collection registerClass:[changjingCell class] forCellWithReuseIdentifier:@"cell"];
    
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 刷新
//        self.isSj = NO;
        [self data];
        
        //新版本是 MJ_header
        [_collection.mj_header endRefreshing];
        
    }];
    //设置刷新控件
    self.collection.mj_header = header;
    
 
    
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        _i++;
      
        
//        self.isSj = YES;
        
        NSString *str =[NSString stringWithFormat:@"http://api.izhangchu.com/?size=20&page=%ld&methodName=SceneList&version=4.3&",_i];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager POST:str parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSArray *arr = responseObject[@"data"][@"data"];
            
           NSMutableArray *tepm = [changjingMOble modelHanderWithArray:arr];
            
            for (changjingMOble *m in tepm) {
                [self.dataArr addObject:m];
            }
            
            
            [_collection reloadData];
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        

        
         [_collection.mj_footer endRefreshing];
    }];
    self.collection.mj_footer = footer;
    
    
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    changjingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    changjingMOble *m = _dataArr[indexPath.row];
    [cell.tupian sd_setImageWithURL:[NSURL URLWithString: m.scene_background]];
    cell.zi.text = m.scene_title;
    cell.cai.text = [NSString stringWithFormat:@"%@ 道菜",[m.dish_count stringValue]];
    if ([m.is_new intValue] == 1) {
        cell.new1.image = [UIImage imageNamed:@"new"];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChangjingWebView *dierye = [[ChangjingWebView alloc]init];
    changjingMOble *m = _dataArr[indexPath.row];
    dierye.str = [m.scene_id stringValue];
    [self.navigationController pushViewController:dierye animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
